<?php


namespace app\admin\controller;


use app\common\model\Movie as MovieModel;
use think\Controller;
use think\Db;
use think\facade\Request;
use app\common\model\Product as ProductModel;
use app\common\model\ProductCps as ProductCpsModel;
class Product extends  Controller
{
    protected $middleware = [
        'app\http\middleware\checkAdminToken::class'=> ['except' 	=> ['moveTestToProduct','pushTestData'] ],
    ];

    //导入全部题目到数据库
    public function pushTestData()
    {
        $s=201      ;
        $e=2000    ;

        for ($i=$s;$i<$e;$i++){
         $res=   $this->moveTestToProduct($i);

         if($res){
             echo  $i.'成功';
             echo  "<br>";
         }else{
             echo  $i.'失败' ;
             echo  "<br>";
         }
        }
//        $this->result('',200,'成功');
    }


    //修改题目
    public function updateTest()
    {
        $test_id=Request::param('id');
        $logo=Request::param('logo');
        $name=Request::param('name');
        $decs=Request::param('decs');
        $count=Request::param('count');
        $recommend=Request::param('recommend');
        $price=Request::param('price');

        //cpm模式
        $type=Request::param('type','cpm');

        if($type=='cpm'){
            $res= ProductModel::where('id',$test_id)->update([
                'logo'=>$logo,
                'name'=>$name,
                'decs'=>$decs,
                'count'=>$count,
                'recommend'=>$recommend,
                'price'=>$price
            ]);
        }else{
            $res= ProductCpsModel::where('id',$test_id)->update([
                'logo'=>$logo,
                'name'=>$name,
                'decs'=>$decs,
                'count'=>$count,
                'recommend'=>$recommend,
                'price'=>$price
            ]);
        }

     $this->result($res,200,'更新成功');

    }

    //将测试题库的题目移入正式版题库
    public function moveTestToProduct($id )
    {
        $data=Db::table('test_data')->where('id',$id)->find();

        if($data==null){
            echo '题目不存在';
            return false;
        }

        //目标数组
        $list=[ ];

        //将数据转化为数组
        $old_list= json_decode($data['data']) ;

        foreach ($old_list as $key => $item) {

            //选项数组
            $a=[];

            foreach ($item->testAnswerDtos as $key2=> $item2) {

                //如果为数字，表示跳转到选项，如果为字母，表示跳转到答案
                if(is_numeric($item2->jumpAnsId)){
                    $type='opt';
                }else{
                    $type='answer';
                }

                array_push($a,[
                    'opt'=>$item2->content,
                    'jumpAnsIndex'=> $item2->jumpAnsId,
                    'value'=>0,
                    'to'=>$type
                ]);
            }

            array_push($list,[
                "q"=>$item->content,
                "a"=>$a
            ]);
        }

         $res=[
             'reslut'=>json_decode($data['answer']) ,
             'list'=>  $list
         ];

        try{
            //去掉没有结果的题目
            if(count($res['reslut'])<1 ){
                echo $id.'没有结果';
                 return false;
//                $this->result( $res['reslut'],200,'该题目没结果');
            }
        }catch (\Exception $e){
//            $this->result( $res['reslut'],200,'该题目没结果');
//            return false;
        }


        $res2=Db::table('product')->insert([
            'name'=>$data['name'],
            'test_type'=>'jump',
            'decs'=>$data['tipsTitle'],
            'status'=>0,
            'data'=>json_encode($res,JSON_UNESCAPED_UNICODE)
        ]);

//         $this->result($res,200);
    }

    //获取产品列表
    public function index()
    {
        $status = Request::param('status', 0);
        $page = Request::param('page', 1);
        $pageSize = Request::param('pageSize', 20);           //哪一天的数据

        //检索条件
        $where=[];
        if($status!=-1){
         array_push($where, ['status','=',$status]);
        }

        $page=Request::param('page',1);

        //cpm模式
        $type=Request::param('type','cpm');
        if($type=='cpm'){
            $list=ProductModel::where($where)->page($page,20)->field(['data'],true)->select();
            $count = ProductModel::where($where) ->count();
        }else{
            $list=ProductCpsModel::where($where)->page($page,20)->field(['data'],true)->select();
            $count = ProductCpsModel::where($where) ->count();
        }

        $data = [
            'list' => $list,
            'page_num' => ceil($count / $pageSize),
            'page' => $page
        ];

        $this->result($data, 200, '成功');
//        $this->result($list,200,'成功');
    }

    //更改状态
    public function changeStatus()
    {
        $status = Request::param('status', 0);
        $product_id = Request::param('product_id', 0);
        //cpm模式
        $type=Request::param('type','cpm');

        if($type=='cpm'){
            $res = ProductModel::where('id', $product_id)
                ->update([
                    "status" => $status
                ]);
        }else{
            $res = ProductCpsModel::where('id', $product_id)
                ->update([
                    "status" => $status
                ]);
        }
        if ($res) {
            $this->result('', 200, '成功');
        } else {
            $this->result('', 400, '失败');
        }
    }


}
